<?php

namespace app\common\services;
use Webman\Push\Api;

class PushService{
    public $session_prefix = 'session-';

    public $chat_prefix = 'chat-';

    public function getApi(){
        $api = new Api(
            config('plugin.webman.push.app.api'),
            config('plugin.webman.push.app.app_key'),
            config('plugin.webman.push.app.app_secret')
        );
        return $api;
    }
    /**
     * @desc: push session message
     * @author: xiaohp
     * @param {*} $session_id
     * @param {*} $message
     */
    public function pushSessionMessage($user_id, $message){
        $channel = $this->session_prefix.$user_id;
        return $this->push($channel, 'message', $message);
    }
    /**
     * @desc: push chat message
     * @author: xiaohp
     * @param {*} $session_id
     * @param {*} $message
     */
    public function pushChatMessage($user_id, $message){
        $channel = $this->chat_prefix.$user_id;
        return $this->push($channel, 'message', $message);
    }
    /**
     * @desc: push
     * @author: xiaohp
     * @param {*} $channel
     * @param {*} $event
     * @param {*} $message
     */
    private function push($channel,$event,$message){
        $api = $this->getApi();
        $res = $api->trigger($channel, $event,$message);
        return $res;
    }
}
